package com.blog.controller;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.blog.pojo.MCategory;
import com.blog.pojo.MLy;
import com.blog.pojo.MUser;
import com.blog.service.IBlogService;
import com.blog.service.ICommentService;
import com.blog.vo.DetailVo;
import com.blog.vo.MBlogAndCategoryVo;
import com.blog.vo.MBlogVo;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontProvider;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerHelper;


@Controller
@RequestMapping("/")
public class portalController {
	
	@Autowired
	private IBlogService service;
	
	@Autowired
	private ICommentService commentService;
	
	
	/************ 前台 ************/
	@RequestMapping("/index")
	public String getContent(Model model){
		
		MBlogAndCategoryVo  vo = service.query2();

		//吧 博客具体内容list 添加到 model 中
		List<MBlogVo> blogVoList = vo.getBlogVoList();
		model.addAttribute("blogVoList",blogVoList );
		
		//吧 分类list 添加到 model 中
		List<MCategory> caterogyList = vo.getCaterogyList();
		model.addAttribute("caterogyList", caterogyList);
		
		//吧 用户信息 添加到 model 中
		MUser user = vo.getUser();
		model.addAttribute("user", user);
		
		
		return "forward:/index.jsp";
	}
	
	
	//博客详情
	@RequestMapping("/detail")
	public String detail(Model model,int id){
		
		DetailVo detailVo = service.getDetail(id);
		
		//吧 博客具体内容list 添加到 model 中
		MBlogVo blogVo = detailVo.getBlogVo();
		model.addAttribute("blog",blogVo );
		
		//吧 分类list 添加到 model 中
		List<MCategory> caterogyList = detailVo.getCaterogyList();
		model.addAttribute("categoryList", caterogyList);
		
		//吧 用户信息 添加到 model 中
		MUser user = detailVo.getUser();
		model.addAttribute("user", user);
		
		
		//吧 上一篇博客和下一篇博客的名字 和 id添加 到 model 中
		model.addAttribute("privName",detailVo.getPriviousName());
		model.addAttribute("privId",detailVo.getBlogVo().getId()-1);
		model.addAttribute("nextName",detailVo.getNextName());
		model.addAttribute("nextId",detailVo.getBlogVo().getId()+1);
		
		return "forward:/detail.jsp";
	}
	
	

	
	
	//通过分类id查询  blogVo列表
	@RequestMapping("/category")
	public String getCat(Model model,Integer categoryid){
		
		MBlogAndCategoryVo  vo = service.queryBlogByCat(categoryid);
		
		//吧 博客具体内容list 添加到 model 中
		List<MBlogVo> blogVoList = vo.getBlogVoList();
		model.addAttribute("blogVoList",blogVoList );
		
		
		
		//吧 分类list 添加到 model 中
		List<MCategory> caterogyList = vo.getCaterogyList();
		model.addAttribute("caterogyList", caterogyList);
		
		//吧 用户信息 添加到 model 中
		MUser user = vo.getUser();
		model.addAttribute("user", user);
		
		return "forward:/index.jsp";
	}
	
	
	//通过名称模糊查询  blogVo列表
	@RequestMapping("/categoryname")
	public String getCatByName(Model model,String title){
		
		MBlogAndCategoryVo  vo = service.queryBlogByName(title);
		
		//吧 博客具体内容list 添加到 model 中
		List<MBlogVo> blogVoList = vo.getBlogVoList();
		model.addAttribute("blogVoList",blogVoList );
		
		
		
		//吧 分类list 添加到 model 中
		List<MCategory> caterogyList = vo.getCaterogyList();
		model.addAttribute("caterogyList", caterogyList);
		
		//吧 用户信息 添加到 model 中
		MUser user = vo.getUser();
		model.addAttribute("user", user);
		
		return "forward:/index.jsp";
	}
	
	
	//将新留言插到数据库中，并查询所有的评论然后装到model中。
	@RequestMapping("/comment")
	public String commnet(String comments,String name, Model model){
		
		List<MLy> lists = commentService.insertAndSelect(comments,name);
		
		model.addAttribute("lys", lists); 
		return "forward:/connect.jsp";
	}
	
	@RequestMapping("/comments")
	@ResponseBody
	public List<MLy> commnets(String comments,String name){
		
		return commentService.insertAndSelect(comments,name);
	}
	
	
	@RequestMapping("/aboutme")
	public String aboutme(Model model){
		
		return "forward:/aboutme.jsp";
	}
	
	@RequestMapping("/connect")
	public String connect(Model model){
		
		List<MLy> lists = commentService.selectAll();
		
		model.addAttribute("lys", lists);
		return "forward:/connect.jsp";
	}
	
	//点赞
	@RequestMapping("/praise")
	@ResponseBody
	public Map praise(Integer id,Integer type){
		
		Map map = commentService.praise(id,type);
		
		return map;
	}
	
	
	//打印功能   使用itext7 ， 弃用
	/*@RequestMapping("/print")
	@ResponseBody
	public Map print(String title,String content) throws Exception{
		
		
		Map map = new HashMap();
		 
		// 模板路径
		String templatePath = "D:\\template.pdf";
		
		//生成的新文件路径
		String newPath = "D:\\"+title+".pdf";
		
		
		PdfDocument pdf = new PdfDocument(new PdfReader(templatePath), new PdfWriter(newPath));
		PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
		
		form.getField("title").setValue(title);
		form.getField("content").setValue(content);
		
		form.flattenFields(); //设置表单域不可更改
		
		pdf.close();
		
		//System.out.println("title="+title +", content ="+ content );
		map.put("status", 0);
		return map;
	}
	*/
	
	@RequestMapping("/print")
	@ResponseBody
	public Map print(String title,String content,String link) throws Exception{
		
		//传过来的content就不用了，没法用，标签都没了。
		//思路： 首先把content从数据库中查出来，然后将图片链接改变一下，改变成ftp服务器上的图片的链接即可。
		
		//detail?id=${blog.id }
		String[] strs = link.split("=");
		//System.out.println(strs[0]+","+strs[1]);
		
		Integer id = Integer.valueOf(strs[1]);
		
		Map map = service.printById(id,title,link);
		
		
		return map;
	}
	
	
}
